﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.IO.IsolatedStorage;
using WP_Customer_ShopMob.ViewModels;
using System.Collections.ObjectModel;

namespace WP_Customer_ShopMob.Views
{
    public partial class CategoriesPage : PhoneApplicationPage
    {
        WP_Customer_ShopMob.Model.Repository repo = new WP_Customer_ShopMob.Model.Repository();
        public CategoriesPage()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            string idCateg;
            NavigationContext.QueryString.TryGetValue("id", out idCateg);
            long id = long.Parse(idCateg);
            CategorieViewModel cvm = Helper.IsolatedStorage.Load<ObservableCollection<CategorieViewModel>>("categories").SingleOrDefault(x => x.Id == id);
            if (!cvm.IsDataLoaded)
            {
                cvm.LoadData(NavigationService);
            }
            this.DataContext = cvm;
        }

        private void lst_categorie_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            /*if (lst_categorie.SelectedItem != null)
            {
                string id = (lst_categorie.SelectedItem as CategorieViewModel).IdCategorie;
                NavigationService.Navigate(new Uri("/Views/SousCategoriesPage.xaml?id=" + id, UriKind.Relative));
                lst_categorie.SelectedItem = null;
            }*/
        }

        private void lst_article_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            if (lst_articles.SelectedItem != null)
            {
                long id = (lst_articles.SelectedItem as ArticleViewModel).IdArticle;
                NavigationService.Navigate(new Uri("/Views/ArticlesPage.xaml?id=" + id, UriKind.Relative));
                lst_articles.SelectedItem = null;
            }
        }

        private void pivot_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            /*if (!(pivot.SelectedItem as CategorieViewModel).IsDataLoaded)
            {
                (pivot.SelectedItem as CategorieViewModel).LoadData();
            }*/
        }
    }
}